package com.right.health.app.ui.me.adapter;

import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.magic.basic.adapter.BaseAbsAdapter;
import com.right.health.app.R;
import com.right.health.app.able.ISampleTextWatcher;
import com.right.health.app.bean.ReviewReminderInfo;

/*
 * Created by hopeliao on 2019-09-24
 *
 */
public class ReviewReminderAddAdapter extends BaseAbsAdapter<ReviewReminderInfo> implements View.OnTouchListener, View.OnFocusChangeListener {

    public int mSelectedEditTextPosition = -1;

    private View.OnClickListener mClickListener;

    private MyTextWatcher mTextWatcher;
    private DescTextWatcher mDescTextWatcher;

    public void setOnClickListener(View.OnClickListener listener) {
        this.mClickListener = listener;
    }

    public ReviewReminderAddAdapter(Context context) {
        super(context);

        mTextWatcher = new MyTextWatcher();
        mDescTextWatcher = new DescTextWatcher();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.review_reminder_add_item, null);
            viewHolder = new ViewHolder();

            viewHolder.nameEt = (EditText) convertView.findViewById(R.id.et_name);
            viewHolder.chooseTimeTv = (TextView) convertView.findViewById(R.id.tv_choose_time);
            viewHolder.endTimeTv= (TextView) convertView.findViewById(R.id.tv_end_time); //复诊结束时间
            viewHolder.chooseCycleTv = (TextView) convertView.findViewById(R.id.tv_choose_cycle); //提醒周期
            viewHolder.descEt = (EditText) convertView.findViewById(R.id.et_content);
            viewHolder.delTv = (TextView) convertView.findViewById(R.id.tv_del); //删除按钮
            viewHolder.tipsTimeTv = (TextView) convertView.findViewById(R.id.tv_tips_time); //提醒时间

            if (mClickListener != null) {
                viewHolder.chooseTimeTv.setOnClickListener(mClickListener);
                viewHolder.chooseCycleTv.setOnClickListener(mClickListener);
                viewHolder.endTimeTv.setOnClickListener(mClickListener);
                viewHolder.delTv.setOnClickListener(mClickListener);
                viewHolder.tipsTimeTv.setOnClickListener(mClickListener);
            }
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tipsTimeTv.setTag(position);
        viewHolder.descEt.setTag(position);
        viewHolder.chooseTimeTv.setTag(position);
        viewHolder.chooseCycleTv.setTag(position);
        viewHolder.endTimeTv.setTag(position);
        viewHolder.nameEt.setTag(position);
        viewHolder.delTv.setTag(position);

        viewHolder.nameEt.removeTextChangedListener(mTextWatcher);
        viewHolder.nameEt.setOnTouchListener(this);
        viewHolder.nameEt.setOnFocusChangeListener(this);

        viewHolder.descEt.removeTextChangedListener(mDescTextWatcher);
        viewHolder.descEt.setOnTouchListener(this);
        viewHolder.descEt.setOnFocusChangeListener(this);

        ReviewReminderInfo info = mDataSource.get(position);

        viewHolder.nameEt.setText(info.visitagainname);
        viewHolder.descEt.setText(info.visitdesc);
        viewHolder.chooseTimeTv.setText(info.nextvisittime);
        viewHolder.endTimeTv.setText(info.visitendtime);
        viewHolder.chooseCycleTv.setText(info.visitTypeText);
        viewHolder.tipsTimeTv.setText(info.remindtypeText);

        return convertView;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            EditText editText = (EditText) v;
            mSelectedEditTextPosition = (int) editText.getTag();

            Log.e("TAG", "touch mSelectedEditTextPosition = " + mSelectedEditTextPosition);

            if(v.getId() == R.id.et_name) {
                mTextWatcher.mCurrIndex = mSelectedEditTextPosition;
                editText.addTextChangedListener(mTextWatcher);
                editText.removeTextChangedListener(mDescTextWatcher);
                mDescTextWatcher.mCurrIndex = -1;
            } else {
                mTextWatcher.mCurrIndex = -1;

                mDescTextWatcher.mCurrIndex = mSelectedEditTextPosition;
                editText.addTextChangedListener(mDescTextWatcher);

                editText.removeTextChangedListener(mTextWatcher);
            }
        }
        return false;
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        EditText editText = (EditText) v;
        if (hasFocus) {
            if(v.getId() == R.id.et_name) {
                mTextWatcher.mCurrIndex = (int) editText.getTag();
                mDescTextWatcher.mCurrIndex = -1;
            } else {
                mTextWatcher.mCurrIndex = -1;
                mDescTextWatcher.mCurrIndex = (int) editText.getTag();
            }

        }
    }

    private class DescTextWatcher extends ISampleTextWatcher {

        public int mCurrIndex;

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (mSelectedEditTextPosition != -1 && mSelectedEditTextPosition == mCurrIndex) {
                ReviewReminderInfo itemTest = (ReviewReminderInfo) getItem(mSelectedEditTextPosition);
                itemTest.visitdesc = s.toString();

                Log.e("TAG", "mCurrIndex = " + mCurrIndex + ",desc = " + s.toString());

                mDataSource.set(mCurrIndex, itemTest);
            } else {
                Log.e("TAG", "pos = " + mSelectedEditTextPosition + ",currIndex = " + mCurrIndex + ",str="+s.toString());
            }
        }
    }

    private class MyTextWatcher extends ISampleTextWatcher {

        public int mCurrIndex;

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (mSelectedEditTextPosition != -1 && mSelectedEditTextPosition == mCurrIndex) {
                ReviewReminderInfo itemTest = (ReviewReminderInfo) getItem(mSelectedEditTextPosition);
                itemTest.visitagainname = s.toString();

                Log.e("TAG", "mCurrIndex = " + mCurrIndex + ",name = " + s.toString());

                mDataSource.set(mCurrIndex, itemTest);
            } else {
                Log.e("TAG", "pos = " + mSelectedEditTextPosition + ",currIndex = " + mCurrIndex + ",str="+s.toString());
            }
        }
    }

    private class ViewHolder {
        private ImageView contentImage;
        private EditText nameEt, descEt;
        private TextView chooseTimeTv;
        private TextView endTimeTv;
        private TextView chooseCycleTv;
        private TextView delTv;
        private TextView tipsTimeTv;
    }
}
